package template.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import org.apache.log4j.Logger;

/**
 * 用于代替clone方法复制对象的工具类
 * @author mengyang
 *
 */
public class CopyUtils {
	
	private static final Logger logger = Logger.getLogger(CopyUtils.class);
	
	public static Object copy(Object oldObj) {
		Object obj = null;
		if(oldObj != null){
			try {
				ByteArrayOutputStream bos = new ByteArrayOutputStream();
				ObjectOutputStream out = new ObjectOutputStream(bos);
				out.writeObject(oldObj);
				out.flush();
				out.close();
	
				ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
				ObjectInputStream in = new ObjectInputStream(bis);
				obj = in.readObject();
			} catch (IOException e) {
				logger.error(e);
			} catch (ClassNotFoundException cnfe) {
				logger.error(cnfe);
			}
		}
		return obj;
	}
}
